/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package moviedownloader5;

import com.mycompany.moviedownloader.DbModel.Genre;
import com.mycompany.moviedownloader.DbModel.Title;
import com.mycompany.moviedownloader.DbModel.Origin;
import com.mycompany.moviedownloader.DbModel.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author martin
 */
public class JPolievkaExtractor {

    private Document moviePage;

    public JPolievkaExtractor(Document moviePage) {
        Objects.requireNonNull(moviePage, "moviePage  cant be null");
        this.moviePage = moviePage;
    }

    /* public String getMoviePage() {
     String tmp = movieName.replace(' ', '+');
     Document doc = XMLFileUtils.loadDocument(WebBrowser.getPage("http://www.csfd.cz/hledat/?q=" + tmp));
     NodeList aNodes = doc.getElementsByTagName("a");
     for (int i = 0; i < aNodes.getLength(); i++) {
     Element el = (Element) aNodes.item(i);
     if (movieName.equals(el.getTextContent())) {
     String page = "http://www.csfd.cz" + el.getAttribute("href");
     moviePage = XMLFileUtils.loadDocument(WebBrowser.getPage(page));
     return page;
     }
     }
     return null;
     }*/
    private Element extractCastElement() {
        Elements elements = moviePage.getElementsByTag("div");
        for (int i = 0; i < elements.size(); i++) {
            Element el = (Element) elements.get(i);
            if ("creators".equals(el.attr("class"))) {
                return el;
            }
        }
        return null;
    }

    private List<Person> extractCast(Element el) {
        List<Person> result = new ArrayList<>();
        if (el == null) {
            return result;
        }

        Elements h4 = el.getElementsByTag("h4");
        for (int i = 0; i < h4.size(); i++) {
            Element div = (Element) h4.get(i);
            // if ("Hrají:".equals(div.getTextContent())) {  // iba herci
            Element divActors = (Element) div.parent();
            Elements actorNodes = divActors.getElementsByTag("a");
            for (int j = 0; j < actorNodes.size(); j++) {
                Element actor = (Element) actorNodes.get(j);
                Person tmp = new Person();
                tmp.setName(actor.text());
                tmp.setCsfdAdress("http://www.csfd.cz" + actor.attr("href"));
                result.add(tmp);
            }
            // }
        }
        return result;
    }

    public List<Person> extractCast() {
        return extractCast(extractCastElement());
    }

    public Origin extractOrigin() {
        Elements pNodes = moviePage.getElementsByTag("p");
        for (int i = 0; i < pNodes.size(); i++) {
            Element tmp = (Element) pNodes.get(i);
            if ("origin".equals(tmp.attr("class"))) {
                String info = tmp.text();
                String[] split = info.split(",");
                //if (split.length != 3) {
                //  throw new MovieParsingException("unexpected format at movie origin:[" + info + "]");
                //}
                Origin result = new Origin();
                for (int j = 0; j < split.length; j++) {
                    String string = split[j];
                }
                result.setCountry(split[0]);
                if (split.length > 1) {
                    result.setReleaseYear(Integer.parseInt(getContentByRegex("\\d+", split[1])));
                }

                if (split.length > 2) {
                    result.setDuration(Integer.parseInt(getContentByRegex("\\d+", split[2])));

                }
                return result;
            }
        }
        return null;
    }

    private static String getContentByRegex(String regex, String text) {
        Matcher match = Pattern.compile(regex).matcher(text);
        if (match.find()) {
            return match.group(0);
        }
        return null;
    }

    public List<Title> extractNames() {
        List<Title> result = new ArrayList<>();
        Elements ulList = moviePage.getElementsByTag("ul");
        for (int i = 0; i < ulList.size(); i++) {
            Element ul = (Element) ulList.get(i);
            if ("names".equals(ul.attr("class"))) {
                Elements namesList = ul.getElementsByTag("li");
                for (int j = 0; j < namesList.size(); j++) {
                    Element li = (Element) namesList.get(j);
                    String country = ((Element) li.getElementsByTag("img").get(0)).attr("alt");
                    String title = ((Element) li.getElementsByTag("h3").get(0)).text();
                    result.add(new Title(country, title));
                }
            }
        }
        result.add(new Title("DEFAULT", extractName()));
        return result;
    }

    public String extractName() {
        String movieName = "";
        Elements divList = moviePage.getElementsByTag("div");
        for (int i = 0; i < divList.size(); i++) {
            Element el = divList.get(i);
            if ("info".equals(el.attr("class"))) {
                Elements ul = el.getElementsByTag("ul");
                boolean gotUl = false;
                if ("links".equals(ul.attr("class"))) {
                    gotUl = true;
                }
                if (gotUl) {
                    Element h1 = el.getElementsByTag("h1").get(0);
                    movieName = h1.text();
                }
            }
        }

        return movieName;
    }
    
    
    public String extractFormat(){
        String result = "film";
        Elements h1 = moviePage.getElementsByTag("h1");
        if(h1.isEmpty()){
            return result;
        }
        Elements span = h1.get(0).getElementsByTag("span");
        if(!span.isEmpty()){
            result = span.get(0).text().replace("(", "").replace(")", "");
        }
        return result.toLowerCase();
    }

    public List<Genre> extractGenres() {
        List<Genre> result = new ArrayList<>();
        Elements pList = moviePage.getElementsByTag("p");
        for (int i = 0; i < pList.size(); i++) {
            Element tmp = ((Element) pList.get(i));
            if ("genre".equals(tmp.attr("class"))) {
                String[] genres = tmp.text().split("/");
                for (String string : genres) {
                    result.add(new Genre(string.replaceAll(" ", "")));
                }
            }
        }
        return result;
    }

    public int extractRating() {
        List<String> result = new ArrayList<>();
        Elements pList = moviePage.getElementsByTag("div");
        for (int i = 0; i < pList.size(); i++) {
            Element tmp = ((Element) pList.get(i));
            if ("rating".equals(tmp.attr("id"))) {
                String content = ((Element) tmp.getElementsByTag("h2").get(0)).text();
                content = content.replace("%", "");
                if (!content.isEmpty()) {
                    return Integer.parseInt(content);
                }
            }
        }
        return 0;
    }

    public String extractCSSFDAdress() {
        Elements links = ((Element) moviePage.getElementsByTag("head").get(0)).getElementsByTag("link");
        for (int i = 0; i < links.size(); i++) {
            Element el = (Element) links.get(i);
            if ("canonical".equals(el.attr("rel"))) {
                return el.attr("href");
            }
        }
        return null;
    }

}
